import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		orgid:null,
		hasLogin: false,
		loginProvider: "",
		openid: null,
		//科目列表
		subjectList: null,
		//机构列表
		orgList: null,
		//教师列表
		empList: null,
		//教室列表
		roomList: null,
		//课程状态列表
		classstatusList: null,

	},
	getters: {
		subjectList: state => {
			return state.subjectList;
		},
		orgList: state=>{
			return state.orgList;
		},
		empList: state=>{
			return state.empList;
		},
		roomList :state =>{
			return state.roomList;
		},
		classstatusList: state=>{
			return state.classstatusList;
		}
	},
	mutations: {
		setSubList(state, subList) {
			state.subList = subList;
		},
		setMyOrg(state,id){
			state.orgid=id;
			
		},
		setOrgList(state, orgList) {
			state.orgList = orgList;
		},
		setEmpsList(state, empsList) {
			state.empsList = empsList;
		},
		setRoomList(state, roomList) {
			state.roomList = roomList;
		},
		setClassstatusList(state, classstatusList) {
			state.classstatusList = classstatusList;
		},
		login(state, provider) {
			state.hasLogin = true;
			state.loginProvider = provider;
		},
		logout(state) {
			state.hasLogin = false
			state.openid = null
		},
		setOpenid(state, openid) {
			state.openid = openid
		}
	},
	actions: {
		//获取课程状态列表
		getClsStatus: async function({
				commit
			}, data) {
				const res = await getClsStatus();
				if (res.success == true) {
					commit('setClassstatusList', res.data.values);
				} else {

				}
			},
			//获取科目列表
			getSubjects: async function({
					commit
				}, data) {
					const res = await getSubject();
					if (res.success == true) {
						commit('setSubList', res.data.values);
					} else {

					}
				},
				//获取组织列表
				getOrgs: async function({
						commit
					}, data) {
						const res = await getOrgs();
						if (res.success == true) {
							commit('setOrgs', res.data.values)
						} else {

						}
					},
					//获取教室列表
					getRooms: async function({
							commit
						}, data) {
							const res = await getRooms();
							if (res.success == true) {
								commit('setRooms', res.data.values);
							} else {

							}

						},
						//获取老师列表
						getEmps: async function({
								commit
							}, data) {
								const res = await getEmps();
								if (res.success == true) {
									commit('setEmps', res.data.values);

								} else {

								}
							},
							// lazy loading openid
							getUserOpenId: async function({
								commit,
								state
							}) {
								return await new Promise((resolve, reject) => {
									if (state.openid) {
										resolve(state.openid)
									} else {
										uni.login({
											success: (data) => {
												commit('login')
												setTimeout(function() { //模拟异步请求服务器获取 openid
													const openid = '123456789'
													console.log('uni.request mock openid[' + openid + ']');
													commit('setOpenid', openid)
													resolve(openid)
												}, 1000)
											},
											fail: (err) => {
												console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
												reject(err)
											}
										})
									}
								})
							}
	}
})

export default store
